package zhuyun.code.launcheractivity.view.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_homepage_friend.*
import kotlinx.android.synthetic.main.fragment_homepage_friend.view.*

import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.model.Constants
import zhuyun.code.launcheractivity.model.NetworkRequest
import zhuyun.code.launcheractivity.model.ResponseDataHandler
import zhuyun.code.launcheractivity.model.data.BaseResponse
import zhuyun.code.launcheractivity.model.data.CircleListBean
import zhuyun.code.launcheractivity.model.data.Level0Item
import zhuyun.code.launcheractivity.view.interfaces.MultiItemEntity
import zhuyun.code.launcheractivity.view.adapter.HomepageFriendAdapter


/**
 * A simple [Fragment] subclass.
 * Use the [HomepageFriendFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class HomepageFriendFragment : Fragment(),View.OnClickListener {
    private var mContentView:View? = null
    private val list = mutableListOf<MultiItemEntity>()
    private lateinit var friendAdapter:HomepageFriendAdapter




    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        if(mContentView == null){
            mContentView = inflater.inflate(R.layout.fragment_homepage_friend, container, false)
            visitUserId = arguments!!.getString(Constants.USER_ID)!!
            initViews()
            initData()
        }
        return mContentView

    }

    private fun initViews(){
        friendAdapter = HomepageFriendAdapter(list,isAllExpand,mContentView!!.rootView.iv_expand)
        val layout = LinearLayoutManager(activity)
        mContentView!!.rootView.rv.layoutManager = layout
        mContentView!!.rootView.rv.adapter = friendAdapter
        mContentView!!.rootView.iv_expand.setOnClickListener(this)
    }

    private fun initData(){
        getData()
    }

    private var page = 1
    private var pageSize = 10
    private var visitUserId = ""

    private fun getData(){
        NetworkRequest.getMyDynamicList(0,page,pageSize,visitUserId,"","",
            object :ResponseDataHandler<List<CircleListBean>>(){
                override fun onSuccess(
                    resultData: List<CircleListBean>?,
                    baseResponse: BaseResponse<List<CircleListBean>>
                ) {
                    resultData?.let {
                        list.addAll(generateData(resultData))
                        friendAdapter.expandAll()
                        friendAdapter.notifyDataSetChanged()
                    }
                }

                override fun onFailure(errMsg: String?) {

                }

            })
    }

    private fun generateData(list:List<CircleListBean>):List<MultiItemEntity>{
        val res = mutableListOf<Level0Item>()
        var lv0:Level0Item? = null
        var lastDate = ""
        var lastYear = ""
        for(i in list.indices){
            val item = list[i]
            val date = item.Addtime.split(" ")[0]
            val temp = date.split("-")
            val year = temp[0]
            val month = temp[1]
            val day = temp[2]
            if(i == 0){
                lv0 = Level0Item(year,"$month.$day")
                lv0.titleVisible = true
                lv0.setExpanded(false)
                lv0.addSubItem(item)
                lastYear = year
            } else {
                if(date == lastDate){
                    lv0!!.addSubItem(item)
                } else {
                    res.add(lv0!!)
                    lv0 = Level0Item(year,"$month.$day")
                    lv0.titleVisible = year != lastYear
                    lv0.setExpanded(false)
                    lv0.addSubItem(item)
                    lastYear = year
                }
            }
            lastDate = date

        }
        res.add(lv0!!)
        return res
    }

    private var isAllExpand = true

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.iv_expand->{
                if(isAllExpand){
                    for(i in friendAdapter.itemCount-1 downTo 0){
                        friendAdapter.collapse(i)
                    }
                    iv_expand.setImageResource(R.drawable.icon_freeout)
                    isAllExpand = false
                } else {
                    friendAdapter.expandAll()
                    friendAdapter.notifyDataSetChanged()
                    iv_expand.setImageResource(R.drawable.icon_allup)
                    isAllExpand = true
                }
            }
        }

    }


}
